<template>
  <ul class="navs">
    <li class="home"><RouterLink to="/">首页</RouterLink></li>
    <li v-for="i in list" :key="i.id"  @click="hide(i)" @mousemove="show(i)" @mouseleave="hide(i)"><RouterLink :to="`/category/${i.id}`">{{i.name}}</RouterLink>
      <div class="layer" :class="{open:i.open}">
        <ul>
          <li v-for="sub in i.children" :key="sub.id">
            <RouterLink :to="`/category/sub/${sub.id}`" @click="hide(i)">
              <img :src='sub.picture' alt="">
              <p>{{sub.name}}</p>
            </RouterLink>
          </li>
        </ul>
      </div>
    </li>
  </ul>
</template>

<script>
import { computed } from '@vue/reactivity'
import { useStore } from 'vuex'
export default {
  name: 'HeaderNav',
  setup () {
    const store = useStore()
    const list = computed(() => {
      return store.state.category.list
    })
    // 跳转的时候不会关闭二级类目,通过数据来控制
    // 1:vuex每个分类加上open属性
    // 提供显示隐藏函数  和一个  控制显示隐藏的类名
    const show = (item) => {
      store.commit('category/show', item.id)
    }
    const hide = (item) => {
      store.commit('category/hide', item.id)
    }
    return {
      list,
      show,
      hide
    }
  }
}
</script>

<style lang="less" scoped>
@import '../assets/styles/variable.less';
.navs {
  position: relative;
    width: 820px;
    display: flex;
    justify-content: space-around;
    padding-left: 40px;
    li {
      margin-right: 40px;
      width: 38px;
      text-align: center;
      a {
        font-size: 16px;
        line-height: 32px;
        height: 32px;
        display: inline-block;
      }
      &:hover {
        > a {
          color: @c;
          border-bottom: 1px solid @c;
        }
          // 子分类
        // > .layer {
        //   height: 132px;
        //   opacity: 1;
        // }
      }

    }
  }
  // 子分类
.layer {
  &.open{
      height: 132px;
      opacity: 1;
        }
  width: 1240px;
  background-color: #fff;
  position: absolute;
  left: -200px;
  top: 46px;
  height: 0;
  overflow: hidden;
  // opacity: 0.5;
  box-shadow: 0 0 5px #ccc;
  transition: all .2s .1s;
  z-index: 999;
  ul {
    display: flex;
    flex-wrap: wrap;
    padding: 0 70px;
    align-items: center;
    height: 132px;
    // z-index: 999;
    li {
      width: 110px;
      text-align: center;
      img {
        width: 60px;
        height: 60px;
      }
      p {
        padding-top: 10px;
      }
      &:hover {
        p {
          color: @c;
        }
      }
    }
  }
}
</style>
